package net.intellect.collectPro.businessLogic.payments;

import java.util.List;
import java.util.logging.Level;

import net.intellect.force.ClientSalesforce;
import net.intellect.force.Constants;

import com.sforce.soap.enterprise.sobject.CP_Payment__c;
import com.sforce.soap.enterprise.sobject.Past_Due__c;

public class InterestPaymentMethod extends BasePaymentMethod implements PaymentMethodProcess{
	
	/**
	 * Apply a PaymentMethod (Interest Payment Method to the PastDue)
	 *
	 * @param past_due pastDue
	 * @param payment Payments
	 * @throws Exception
	 */
	public void apply(Past_Due__c past_due, List<CP_Payment__c> paymentList) throws Exception {
		if (null != paymentList){	
			
			consolidatedPayments(past_due, paymentList);			
			
			//OPEN --> RECOVERED
			if (past_due.getPast_Due_Amount_Local__c() >= past_due.getMinimum_Payment_Local__c() &&
				past_due.getPast_Due_Amount_USD__c() >= past_due.getMinimum_Payment_USD__c()){
				
				Past_Due__c past_dueUpd = new Past_Due__c();
				past_dueUpd.setId(past_due.getId());
				past_dueUpd.setStatus__c(Constants.PAST_DUE_STATUS_RECOVERED);
				past_dueUpd.addFieldsToNull("Recovery_Date__c");
				
				try{
					ClientSalesforce.update(past_dueUpd);				
				} catch(Exception e) { 
					logger.log(Level.INFO, "COLLECT/PAYMENTS Error Interest Payment Method update the Past Due Status " +past_due.getId() + "  - End of process");
					throw new Exception("0. Error Interest Payment Method update the Past Due Status: " +past_due.getId() + "  - End of process");
				}				
				//updateActivityPast_due(past_due,PAST_DUE_STATUS_RECOVERED);	
			}else{
				logger.log(Level.INFO, "COLLECT/PAYMENTS Negative Payment in Interest Payment Method for the Past Due " +past_due.getId());
			}

		}
			
	}

}
